package com.smartsoft.activity;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.*;
import com.smartsoft.R;
import com.smartsoft.fragment.EditContactFragment;
import com.smartsoft.fragment.PreviewContactFragment;
import com.smartsoft.fragment.VideoFragment;
import com.smartsoft.model.ContactData;


public class AddressBookActivity extends Activity
        implements EditContactFragment.OnEditContactSubmitListener,
                   PreviewContactFragment.OnAcceptOrBackListener,
                   VideoFragment.OnAddContactListener {

    /**
     * For debug purposes.
     */
    private static final String TAG = AddressBookActivity.class.getName();


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    /**
     * User presses Add Contact button on video fragment.
     * Hide those button and display edit contact page.
     */
    @Override
    public void onContactAddition() {
        Button mAddContact = (Button) getFragmentManager().findFragmentById(R.id.video_fragment)
                                      .getView().findViewById(R.id.add_contact_btn);
        mAddContact.setVisibility(View.GONE);
        EditContactFragment editContactFragment = new EditContactFragment();
        getFragmentManager()
            .beginTransaction()
            .add(R.id.fragments_container, editContactFragment)
            .commit();
    }

    /**
     * Fired when user submits contact data. App transitioned to the
     * preview page.
     *
     * @param contactData contains submitted data
     */
    @Override
    public void onContactSubmitted(ContactData contactData) {
        if (contactData == null) {
            throw new IllegalArgumentException("Submitted contact data must be not null. Check is " +
                    "EditContact fragment passes ContactData object correctly during submition procedure.");
        }
        Log.d(TAG, "Handle contact submition ..." +
                "\nReceived contact data:\n" + contactData.toString());
        PreviewContactFragment previewContactFragment = new PreviewContactFragment();
        previewContactFragment.setContactData(contactData);
        // Dynamically add preview fragment to the activity.
       getFragmentManager()
               .beginTransaction()
               .replace(R.id.fragments_container, previewContactFragment, PreviewContactFragment.class.getName())
               .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
               .addToBackStack(null)
               .commit();
        Log.d(TAG, "EditContact to PreviewContact fragments substitution successfully performed.");
    }

    /**
     * Fired when user accept data on preview page.
     *
     * @param contactData contact data which was accepted by the user
     */
    @Override
    public void onContactAccepted(ContactData contactData) {
        Log.d(TAG, "Accept button pressed");
        Fragment previewFragment = getFragmentManager().findFragmentByTag(PreviewContactFragment.class.getName());
        getFragmentManager()
                .beginTransaction()
                .remove(previewFragment)
                .commit();
        Button mAddContact = (Button) getFragmentManager().findFragmentById(R.id.video_fragment)
                                      .getView().findViewById(R.id.add_contact_btn);
        mAddContact.setVisibility(View.VISIBLE);
        Toast.makeText(this, "Your contact data has been successfully saved", Toast.LENGTH_LONG).show();
    }

    /**
     * Fired when user presses Back button. It should go back
     * to the edit contact page.
     */
    @Override
    public void onContactRejected() {
        Log.d(TAG, "Process Back button pressing");
        // Go to edit contact page
        getFragmentManager().popBackStack();
    }

}